package com.evolved.automata.android.bots.speechbot;
import java.util.*;

public class MapComparer 
{
	Hashtable<String,String> baseMap;
	Hashtable<String,String> compMap;
	
	Hashtable<String,String> changedValueMap;
	Hashtable<String,String> deletedKeyMap;
	Hashtable<String,String> addedKeyMap;
	Hashtable<String,String> commonKeyMap;
	
	
	
	public MapComparer(Hashtable<String,String> baseMap, Hashtable<String,String> compMap)
	{
		this.baseMap = baseMap;
		this.compMap=compMap;
		
		changedValueMap = new Hashtable<String, String>();
		deletedKeyMap = new Hashtable<String, String>();
		addedKeyMap = new Hashtable<String, String>();
		commonKeyMap = new Hashtable<String, String>();
		
		String baseValue, compValue;
		for (String baseKey:baseMap.keySet().toArray(new String[0]))
		{
			baseValue=baseMap.get(baseKey);
			
			if (compMap.containsKey(baseKey))
			{
				compValue=compMap.get(baseKey);
				if (!baseValue.equals(compValue))
				{
					changedValueMap.put(baseKey, compValue);
				}
				else 
					commonKeyMap.put(baseKey, baseValue);
			}
			else
			{
				deletedKeyMap.put(baseKey, baseValue);
			}
		}
		
		
		for (String compKey:compMap.keySet().toArray(new String[0]))
		{
			compValue=compMap.get(compKey);
			// Check if  baseKey is in compMap and if so if they are the same
			
			if (!baseMap.containsKey(compKey))
			{
				addedKeyMap.put(compKey, compValue);
			}
		}
		
		
	}
	
	public Hashtable<String,String> getCompMap()
	{
		return compMap;
	}
	
	public Hashtable<String,String> getBaseKeys()
	{
		return (Hashtable<String,String>)baseMap.clone();
	}
	public Hashtable<String,String> getDeletedKeys()
	{
		return (Hashtable<String,String>)deletedKeyMap.clone();
	}
	
	public Hashtable<String,String> getCommonKeys()
	{
		return (Hashtable<String,String>)commonKeyMap.clone();
	}
	
	
	public Hashtable<String,String> getAddedKeys()
	{
		return (Hashtable<String,String>)addedKeyMap.clone();
	}
	public Hashtable<String,String> getChangedKeys()
	{
		return (Hashtable<String,String>)changedValueMap.clone();
	}
	
	public String getTransactionDescription()
	{
		if (compMap.size()==0)
			return "ALL WORLDS CONSISTENT WITH FRAME 1 WILL BE DELETED.  THIS IS IRREVERSIBLE!";
		
		StringBuffer buffer = new StringBuffer();
		for (String deletedKey:deletedKeyMap.keySet().toArray(new String[0]))
		{
			buffer.append(String.format("%1$s was deleted from all worlds consistent with frame 1\r\n", deletedKey));
		}
		for (String addKey:addedKeyMap.keySet().toArray(new String[0]))
		{
			buffer.append(String.format("%1$s was added to all worlds consistent with frame 1\r\n", addKey));
		}
		
		for (String changedKey:changedValueMap.keySet().toArray(new String[0]))
		{
			buffer.append(String.format("%1$s was changed in all worlds consistent with frame 1\r\n", changedKey));
		}
		return buffer.toString();
	}
	
}
